<template>
  <div class="about">
    <h1>{{ id ? '编辑': '新建' }}英雄</h1>
    <el-form label-width="120px" @submit.native.prevent="save">
      <el-tabs type="border-card" value="skills">
        <el-tab-pane label="基础信息" name="first">
          <el-form-item label="名称">
            <el-input v-model="model.name" ></el-input>
          </el-form-item>
          <el-form-item label="称号">
            <el-input v-model="model.title" ></el-input>
          </el-form-item>
          <el-form-item label="类型">
            <el-select v-model="model.categories" multiple >
              <el-option v-for="item of categories"
                :key="item._id"
                :label="item.name"
                :value="item._id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="难度" >
            <el-rate :max="9" show-text style="margin-top: 0.7rem" v-model="model.scores.difficult"></el-rate>
          </el-form-item>
          <el-form-item label="技能" >
            <el-rate :max="9" show-text style="margin-top: 0.7rem" v-model="model.scores.skills"></el-rate>
          </el-form-item>
          <el-form-item label="攻击" >
            <el-rate :max="9" show-text style="margin-top: 0.7rem" v-model="model.scores.attack"></el-rate>
          </el-form-item>
          <el-form-item label="生存" >
            <el-rate :max="9" show-text style="margin-top: 0.7rem" v-model="model.scores.survive"></el-rate>
          </el-form-item>
          <el-form-item label="顺风出装">
            <el-select v-model="model.items1" multiple >
              <el-option v-for="item of items"
                :key="item._id"
                :label="item.name"
                :value="item._id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="逆风出装">
            <el-select v-model="model.items2" multiple >
              <el-option v-for="item of items"
                :key="item._id"
                :label="item.name"
                :value="item._id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="使用技巧">
            <el-input v-model="model.usageTips" type="textarea" :rows="3">
            </el-input>
          </el-form-item>
          <el-form-item label="对抗技巧">
            <el-input v-model="model.battleTips" type="textarea" :rows="3">
            </el-input>
          </el-form-item>
          <el-form-item label="团战思路">
            <el-input v-model="model.teamTips" type="textarea" :rows="3">
            </el-input>
          </el-form-item>
          <el-form-item label="头像">
            <el-upload
              class="avatar-uploader"
              :action="$http.defaults.baseURL + '/upload'"
              :show-file-list="false"
              :on-success="handleAvatarSuccess">
              <img v-if="model.avatar" :src="model.avatar" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item> 
        </el-tab-pane>
        <el-tab-pane label="技能" name="skills">
          <el-button size="small" @click="model.skills.push({})">
            <i class="el-icon-plus"></i> 添加技能
          </el-button>
          <el-row type="flex" style="flex-wrap: wrap">
            <el-col :md="12" v-for="(item, i) in model.skills" :key="i">
              <el-form-item label="名称">
                <el-input v-model="item.name"></el-input>
              </el-form-item>
              <el-form-item label="图标">
                <el-upload
                  class="avatar-uploader"
                  :action="$http.defaults.baseURL + '/upload'"
                  :show-file-list="false"
                  :on-success="handleAvatarSuccess">
                  <img v-if="model.icon" :src="model.icon" class="avatar">
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </el-form-item>
              <el-form-item label="描述">
                <el-input v-model="item.description" type="textarea" :rows="2"></el-input>
              </el-form-item>
               <el-form-item label="小提示">
                <el-input v-model="item.tips" type="textarea" :rows="2"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>
      <el-form-item>
        <el-button type="primary" native-type="submit">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  props: {
    // 这个id是拿到路由里编辑的id
    id: {}
  },
  data(){
    return {
      model: {
        name: '',
        avatar: '',
        scores: {
          difficult: 0,
        },
        skills: []
      },
      categories: [],
      items: [],
    }
  },
  methods: {
    // 图片上传成功函数
    handleAvatarSuccess(res) {
      // 因为vue 双向绑定初始值问题 所以做好用 $set()
      this.model.avatar = res.url
      // this.$set(this.model, 'avatar', res.url)
    },
    // 新建和编辑因为是同一个页面，所以还需要条件判断请求不同
    async save(){
      let res
      if(this.id){
        res = await this.$http.put(`rest/heros/${this.id}`, this.model)
      } else {
        res = await this.$http.post('rest/heros', this.model) 
      }
      this.$router.push('/heros/list')
      this.$message({
        type: 'success',
        message: '保存成功'
      })
    },
    async fetch() {
      const res = await this.$http.get(`/rest/heros/${this.id}`);
      // this.model = res.data
      this.model = Object.assign({}, this.model, res.data)
    },

    // 获取所有下拉框需要的数据
    async fetchCategories() {
      const res = await this.$http.get('/rest/categories')
      this.categories = res.data
    },
    async fetchItems() {
      const res = await this.$http.get('/rest/items')
      this.items = res.data
    },
  },
  created() {
    // 如果能拿到编辑的id 才会请求接口，将分类名称展示到页面上
    this.id && this.fetch()
    this.fetchCategories()
    this.fetchItems()
  }
}
</script>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
  }
  .avatar {
    width: 100px;
    height: 100px;
    display: block;
  }
</style>

